package com.carp.springframework.bean;

import com.carp.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lile
 * @since 2023/4/25
 */
public class ProxyBeanFactory implements FactoryBean<IUserDao> {

    @Override
    public IUserDao getObject() {
        InvocationHandler handler = (proxy, method, args) -> {

            Map<Integer, String> map = new HashMap<>();
            map.put(1, "皮卡丘");
            map.put(2, "憨八龟");
            map.put(3, "猪猪侠");

            return "(代理方法 " + method.getName() + "：" + map.get((int) args[0]) + ")";
        };
        return (IUserDao) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{IUserDao.class}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
